package cn.iocoder.yudao.framework.common.auth;

import com.alibaba.ttl.TransmittableThreadLocal;

import javax.validation.constraints.NotNull;

public class LoginInfoHolder {

    private static final TransmittableThreadLocal<LoginUser> LOGIN_INFO = new TransmittableThreadLocal<>();

    public static void setLoginUser(@NotNull LoginUser loginUser) {
        LOGIN_INFO.set(loginUser);
    }

    public static LoginUser getLoginUser() {
        return LOGIN_INFO.get();
    }

    public static LoginUser getLoginUserDefault() {
        if (LOGIN_INFO.get() == null) {
            LoginUser loginUser = new LoginUser();
            loginUser.setId(0l);
            loginUser.setUserName("SYS");
            return loginUser;
        }
        return LOGIN_INFO.get();
    }

    public static void clearLoginUser() {
        LOGIN_INFO.remove();
    }
}
